home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_06 / vancamp / tdbuf.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-17  |  2.5 KB  |  95 lines

  1. // tdbuf.hpp: TableDataBuffer class (LISTING 4)
  2. #ifndef TDBUF_HPP
  3. #define TDBUF_HPP
  4. #include "tbldata.hpp"
  5.  
  6. // Buffer class for data tables
  7. template <class cellType> class TableDataBuffer:
  8.         public TableData<cellType>
  9. {
  10. public:
  11.     TableDataBuffer (TableData<cellType> *const prev,
  12.             const int nRows, const int nCols):
  13.             TableData<cellType> (prev),
  14.             NumRows (nRows),
  15.             NumCols (nCols)
  16.     {
  17.         // Allocate the buffer
  18.         TableBuf = new cellType[nRows * nCols];
  19.         assert (TableBuf != 0);
  20.         // Allocate storage for row & col headings too.
  21.         RowHeading = new char [nRows * MaxHeadingLen];
  22.         assert (RowHeading != 0);
  23.         ColHeading = new char [nCols * MaxHeadingLen];
  24.         assert (ColHeading != 0);
  25.     }
  26.  
  27.     virtual ~TableDataBuffer (void)
  28.     {
  29.         delete []TableBuf;
  30.         delete []RowHeading;
  31.         delete []ColHeading;
  32.     }
  33.  
  34.     virtual const cellType GetCell (const int row,
  35.             const int col)
  36.     {
  37.         ValidateCoord (row, col);
  38.         return (RefCell (row, col));
  39.     }
  40.  
  41.     virtual void PutCell (const int row, const int col,
  42.             const cellType &value)
  43.     {
  44.         ValidateCoord (row, col);
  45.         RefCell (row, col) = value;
  46.     }
  47.  
  48.     virtual const int GetNumRows (void) const
  49.     { return (NumRows); }
  50.  
  51.     virtual const int GetNumCols (void) const
  52.     { return (NumCols); }
  53.  
  54.     virtual const char &GetRowHeading (const int row)
  55.     {
  56.         ValidateRow (row);
  57.         return (RefRowHeading (row));
  58.     }
  59.  
  60.     virtual const char &GetColHeading (const int col)
  61.     {
  62.         ValidateCol (col);
  63.         return (RefColHeading (col));
  64.     }
  65.  
  66.     virtual void ValidateCoord (const int row,
  67.             const int col) const
  68.     { ValidateRow (row); ValidateCol (col); }
  69.  
  70.     virtual void ValidateRow (const int row) const
  71.     { assert ( (row >= 0 && row < GetNumRows()) ); }
  72.  
  73.     virtual void ValidateCol (const int col) const
  74.     { assert ( (col >= 0 && col < GetNumCols()) ); }
  75.  
  76. protected:
  77.     enum { MaxHeadingLen = 40 };
  78.  
  79.     cellType &RefCell (int row, int col)
  80.     { return (TableBuf[row * GetNumCols() + col]); }
  81.  
  82.     char &RefRowHeading (int row)
  83.     { return (RowHeading[row * MaxHeadingLen]); }
  84.  
  85.     char &RefColHeading (int col)
  86.     { return (ColHeading[col * MaxHeadingLen]); }
  87.  
  88. private:
  89.     int NumRows, NumCols;
  90.     cellType *TableBuf; // buffer holds all cells
  91.     char *RowHeading;   // array of row headings
  92.     char *ColHeading;   // array of col headings
  93. };
  94. #endif
  95.